/*
 * customer权限下banner创建
 * @Author: zfx
 * @Date: 2018-06-19 17:53:51
 */
<style lang="less" rel="stylesheet/less" type="text/less" scoped>
    .v-customer-banner-create {
		height: 74vh;
		overflow-y: scroll;
		&::-webkit-scrollbar {
			display: none;
		}
        .banner-form {
            max-width: 400px;
        }
    }
</style>
<template>
	<div class="v-customer-banner-create">

		<Row
			type="flex"
			justify="start">
			<i-col push="1">
				<Form
					ref="formValidate"
					:model="formValidate"
					:rules="ruleValidate"
					:label-width="labelWidth(130, 120)"
					name="create__form"
					class="banner-form">

					<Form-item
						:label="$t('banner.jump_type')"
						prop="banner_type_id">
						<Select
							v-model="formValidate.banner_type_id"
							clearable
							filterable
							name="create__banner_type_id"
							transfer>
							<Option
								v-for="(item,key) in jumpType"
								:value="String(item.id)"
								:key="key">{{ item.name }}</Option>
						</Select>
					</Form-item>

					<Form-item
						v-if="formValidate.banner_type_id == '2'"
						:label="$t('banner.product_information')"
						prop="product_information">
						<Row>
							<i-col span="22">
								<Select
									v-model="formValidate.product_information"
									name="create__product_information"
									clearable
									filterable
									transfer>
									<Option
										v-for="(item,key) in products"
										:value="item.spu_code.replace(/(^\s*)|(\s*$)/g, '')"
										:key="key">{{ item.name }}</Option>
								</Select>
							</i-col>
							<i-col
								:offset="1"
								span="1">
								<Tooltip
									:content="$t('banner.jump_info')"
									placement="right">
									<a class="help">
										<Icon
											type="help-circled"
											size="20"/>
									</a>
								</Tooltip>
							</i-col>
						</Row>
					</Form-item>

					<Form-item
						:label="$t('banner.specified_type')"
						prop="type">
						<Select
							v-model="formValidate.type"
							clearable
							filterable
							name="create__specified_type"
							transfer>
							<Option
								v-for="(item,key) in specified_data"
								:value="item.value"
								:key="key">{{ item.name }}</Option>
						</Select>
					</Form-item>

					<Form-item
						v-if="formValidate.type == 'Store'"
						:label="$t('banner.store')"
						prop="store">
						<Select
							v-model="formValidate.store"
							multiple
							clearable
							filterable
							name="create__store"
							transfer>
							<Option
								v-for="(item,key) in storesData"
								:value="String(item.id)"
								:key="key">{{ item.name }}</Option>
						</Select>
					</Form-item>

					<Form-item
						v-else-if="formValidate.type == 'DeviceAccount'"
						:label="$t('banner.device_number')"
						prop="device_number">
						<Select
							v-model="formValidate.device_number"
							filterable
							multiple
							clearable
							name="create__device_number"
							transfer>
							<OptionGroup
								v-for="(item,key) in storesData"
								:key="key"
								:label="item.name">
								<Option
									v-for="(value,key) in item.get_device_accounts"
									:value="String(value.id)"
									:key="key">{{ value.account }}</Option>
							</OptionGroup>
						</Select>
					</Form-item>

					<Form-item
						:label="$t('banner.name')"
						prop="name">
						<i-input
							v-model="formValidate.name"
							:placeholder="$t('banner.please_enter_name')"
							name="create__name"
							type="text"/>
					</Form-item>

					<Form-item
						:label="$t('banner.priority')"
						prop="priority">
						<InputNumber
							:max="100000"
							:min="0"
							v-model="formValidate.priority"
							name="create__priority"/>
					</Form-item>

					<Form-item
						:label="$t('banner.thumb')"
						prop="thumb">
						<Uploader
							ref="thumb"
							@on-remove="handleRemove"
							@on-change="handleChange">
							{{ $t('banner.thumb_tip') }}
						</Uploader>
					</Form-item>

					<Form-item>
						<Button
							type="primary"
							name="create__submit"
							@click="handleSubmit('formValidate')">
							{{ $t('common.submit') }}
						</Button>

						<Button
							type="ghost"
							name="create__reset"
							style="margin-left: 8px"
							@click="handleReset('formValidate')">
							{{ $t('common.reset') }}
						</Button>

					</Form-item>

				</Form>
			</i-col>
		</Row>
	</div>
</template>

<script>

import labelWidthMixin from 'src/views/mixins/labelWidthMixin';
import { validateImage } from 'src/validate'; // 引入校验文件
import Uploader from 'src/views/component/Uploader';

export default {
	name: 'BannerCreate',
	components: {
		Uploader,
	},
	mixins: [labelWidthMixin],
	props: {
		jumpType: {
			type: Array,
			default() {
				return [];
			},
		},
		storesData: {
			type: Array,
			default() {
				return [];
			},
		},
		deviceAccounts: {
			type: Array,
			default() {
				return [];
			},
		},
		products: {
			type: Array,
			default() {
				return [];
			},
		},
		loading: {
			type: Boolean,
			default() {
				return false;
			},
		},
	},

	data() {
		return {
			specified_data: [
				{
					name: this.$t('banner.device_number'),
					value: 'DeviceAccount',
				},
				{
					name: this.$t('banner.store'),
					value: 'Store',
				},
			],

			formValidate: {
				banner_type_id: '', // 跳转类型
				product_information: '', // 商品信息
				type: '', // 指定设备类型
				store: [], // 店铺
				device_number: [], // 设备编号
				name: '', // banner名称
				priority: 0, // 优先级
				thumb: '', // banner图
			}, // 新建banner时的表单

			show_detail_btn: false,

			ruleValidate: { // 表单验证规则
				banner_type_id: [
					{
						required: true,
						message: this.$t('validate.required', { field: this.$t('banner.jump_type') }),
						trigger: 'blur,change',
					},
				],
				product_information: [
					{
						required: true,
						message: this.$t('validate.required', { field: this.$t('banner.product_information') }),
						trigger: 'blur,change',
					},
				],
				type: [
					{
						required: true,
						message: this.$t('validate.required', { field: this.$t('banner.specified_type') }),
						trigger: 'blur,change',
					},
				],
				store: [
					{
						required: true,
						type: 'array',
						message: this.$t('validate.required', { field: this.$t('banner.store') }),
						trigger: 'blur,change',
					},
				],
				device_number: [
					{
						required: true,
						type: 'array',
						message: this.$t('validate.required', { field: this.$t('banner.device_number') }),
						trigger: 'blur,change',
					},
				],
				name: [
					{
						required: true,
						message: this.$t('validate.required', { field: this.$t('banner.name') }),
						trigger: 'blur,change',
					},
				],
				priority: [
					{
						required: true,
						type: 'number',
						message: this.$t('validate.required', { field: this.$t('banner.priority') }),
						trigger: 'blur,change',
					},
				],
				thumb: [
					{ required: true, validator: validateImage, trigger: 'blur,change' },
				],

			},

		};
	},

	methods: {
		/**
		 * 删除图片
		 * @return {undefined}
		 */
		handleRemove() {
			this.formValidate.thumb = null;
			this.$refs.formValidate.validateField('thumb');
		},

		/**
		 * 选择图片
		 * @param {Object} file {选择的文件对象集合}
		 * @return {undefined}
		 */
		handleChange({ file }) {
			this.formValidate.thumb = file;
			this.$refs.formValidate.validateField('thumb');
		},

		/**
         * 提交表单
         * @param  {string} name {表单的ref}
         * @return {undefined}
         */
		handleSubmit(name) {
			if (this.loading) return;
			this.$refs[name].validate((valid) => {
				if (valid) {
					this.$emit('post', this.formDataCreate());
				} else {
					this.$Message.error(this.$t('common.exist_empty_field'));
				}
			});
		},

		/**
         * 重置表单
         * @param  {string} name {表单的ref}
         * @return {undefined}
         */
		handleReset(name) {
			this.$refs.thumb.reset();
			this.$refs[name].resetFields();

			this.formValidate.taobao_product_info = {};

			this.formValidate.color_thumb = '';
			// 补充
			this.formValidate.store = [];
			this.formValidate.device_number = [];
		},

		/**
         * 手动拼接表单数据
         * @return {Object} {拼接好的表单数据}
         */
		formDataCreate() {
			const new_object = {
				banner_type_id: this.formValidate.banner_type_id, // 跳转类型
				type: this.formValidate.type, // 指定设备类型
				name: this.formValidate.name, // banner名称
				priority: this.formValidate.priority, // 优先级
				image: this.formValidate.thumb, // banner图
				data: this.formValidate.product_information, // 商品信息
			};

			switch (this.formValidate.type) {
			case 'DeviceAccount':
				new_object.values = this.formValidate.device_number;

				break;
			case 'Store':
				new_object.values = this.formValidate.store;

				break;
			default:
				break;
			}
			const form_data = new FormData();

			Object.keys(new_object).forEach((key) => {
				switch (key) {
				case 'values':

					new_object[key].forEach((tag, index) => {
						form_data.append(`${key}[${index}]`, tag);
					});

					break;

				default:

					form_data.append(key, new_object[key]);

					break;
				}
			});

			return form_data;
		},

	},


};
</script>
